package com.imooc.mymall.exception;

import com.imooc.mymall.domain.common.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <h1>全局异常捕获处理器</h1>
 * @Author: crowsjian
 * @Date: 2020/7/13 21:23
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    /**
     * <h2>捕捉业务异常</h2>
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = BusinessException.class)
    public ResponseResult BusinessExceptionHandle(BusinessException e){
        log.error("捕捉到业务类异常：",e);
        return ResponseResult.failure(e.getCode(),e.getMessage());
    }

    /**
     * <h2>拦截运行时异常</h2>
     * @param e
     */
    @ResponseBody
    @ExceptionHandler(value = RuntimeException.class)
    public ResponseResult runtimeExceptionHandle(RuntimeException e){
        log.error("捕捉到运行时异常：",e);
        return ResponseResult .failure(ErrorCodeEnum.UNKNOWN_ERROR.getCode(),e.getMessage());
    }

    /**
     * <h2>捕捉系统级异常</h2>
     * @param th
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Throwable.class)
    public ResponseResult throwableHandle(Throwable th){
        log.error("捕捉Throwable异常:",th);
        return ResponseResult.failure(ErrorCodeEnum.SYSTEM_ERROR.getCode(),th.getMessage());
    }
}
